package com.liuwei.spring.cloud.framework.feigns.caller;

import cn.hutool.core.util.StrUtil;
import com.liuwei.spring.cloud.common.utils.StringPool;
import com.liuwei.spring.cloud.framework.spring.ApplicationHelper;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.SneakyThrows;

/**
 * @author : evan.liu
 * @version V1.0
 * @Description: TODO
 * @date Date : 2022年05月16日 12:00 上午
 */

public class FeignAuthTokenInterceptor implements RequestInterceptor {

    /**
     * 发起请求添加认证信息
     * @param template
     */
    @SneakyThrows
    @Override
    public void apply(RequestTemplate template) {
        String application = ApplicationHelper.getApplicationName();
        String secret = ApplicationHelper.getApplicationId(application);
        Long now = System.currentTimeMillis();
        //String auth = AesHelper.encode2HexString(String.valueOf(now),secret);
        String auth = "";
        String value = StrUtil.format("{}|{}",application,auth);
        // 通过添加 header 进行内部 feign 调用的鉴权
        template.header(StringPool.Feign_Auth_Strategy,"token");
        template.header(StringPool.Feign_Auth_Token,value);
    }
}
